package tests;

import java.io.IOException;
import java.util.LinkedList;

import plochki.BaiGosho;
import plochki.BaiGrozdan;
import plochki.BaseWorker;
import plochki.BaiPesho;
import plochki.BaiPetkan;
import plochki.BaiTupan;

public class TestMivki {

	//Test function
	public static void makeFileTests() throws IOException {
		//Test all available algorithms against tests files
		testMaistori("tests/MivkiTest1.txt");
		testMaistori("tests/MivkiTest2.txt");
		testMaistori("tests/Mivki_4_Test.txt");
		testMaistori("tests/Mivki_5_Test.txt");
	}
	
	private static void testMaistori(String inputFile) throws IOException {
		
		LinkedList<BaseWorker> workers = new LinkedList<BaseWorker>();
		
		//Make the work in the easy way
		workers.add(new BaseWorker(inputFile));
		//Make the work in the easy way, traverse the figures in reverse order
		workers.add(new BaiPesho(inputFile));
		//Make the work in the easy way, traverse the figures (with even then odd indexes)
		workers.add(new BaiGosho(inputFile));
		//A more complicated approach, uses Collision Cells
		workers.add(new BaiGrozdan(inputFile));
		//A more complicated approach, Collisions traversed in a reversed order
		workers.add(new BaiPetkan(inputFile));
		//A more complicated approach, Collisions traversed first even indexes then odd indexes
		workers.add(new BaiTupan(inputFile));
		
		//Each worker to make his work
		for(BaseWorker worker: workers) {
			worker.deistvai();
		}
		
		BaseWorker fastestWorker = findFastestWorker(workers);
		
		System.err.println("Tests finished on " + inputFile + ", Best worker: " + fastestWorker.name
				+ " Moves: " + fastestWorker.movesMade);
		/*
		//Calculate the difference in %
		double differenceInMoves = Math.abs(100 - baiGrozdan.movesMade*1.0/baiIvan.movesMade*100);

		//Decide which is the best Method
		if (baiIvan.movesMade < baiGrozdan.movesMade) {
			System.out.println(String.format("BaiIvan wins with %.2f%% less work done.", differenceInMoves));
		}
		else {
			System.out.println(String.format("BaiGrozdan wins with %.2f%% less work done.", differenceInMoves));
		}
		*/
	}

	//Get the fastest worker
	public static BaseWorker findFastestWorker(LinkedList<BaseWorker> workers) {
		BaseWorker fastest = workers.pop();
		BaseWorker tempWorker;
		
		while (!workers.isEmpty()) {
			tempWorker = workers.pop();
			if (fastest.movesMade > tempWorker.movesMade) {
				fastest = tempWorker;
			}
		}
		
		return fastest;
	}

}
